package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.ui.activity.HouseInfoActivity;
import com.rongzhe.house.utils.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huang on 2018/3/10.
 */

public class MainRentTypeAdapter extends RecyclerView.Adapter<MainRentTypeAdapter.MyViewHolder> {
    private final Context mContext;
    private static String THUMBNAIL_SUFFIX = null;
    private static final String THUMBNAIL_SUFFIX_FORMAT = "imageView2/1/w/%s/h/%s";

    public MainRentTypeAdapter(Context context) {
        this.mContext = context;
    }


    List<HouseOverview> houseInfos = new ArrayList<>();
    public void setDatas(List<HouseOverview> houseInfos) {
        this.houseInfos = houseInfos;
        notifyDataSetChanged();
    }

    @Override
    public MainRentTypeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final MyViewHolder myViewHolder = new MyViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item_house_info_main, parent, false));
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRentTypeAdapter.MyViewHolder holder, int position) {
        final HouseOverview houseOverview = houseInfos.get(position);
        if (!TextUtils.isEmpty(houseOverview.getCover())) {
            ImageView housePhoto = holder.imageHousePhoto;
            if (TextUtils.isEmpty(THUMBNAIL_SUFFIX)) {
                THUMBNAIL_SUFFIX = String.format(THUMBNAIL_SUFFIX_FORMAT,
                        String.valueOf(UIUtils.dip2px(mContext, 230)),
                        String.valueOf(UIUtils.dip2px(mContext, 150)));
            }
            Picasso.with(mContext).load(houseOverview.getCover() + "?" + THUMBNAIL_SUFFIX)
                    .into(housePhoto);
            holder.textHouseTitle.setText(houseOverview.getName());
            if(houseOverview.getRent() != null) {
                holder.textRent.setText(houseOverview.getRent().toString() + "元/月");
            }
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, HouseInfoActivity.class);
                    intent.putExtra(HouseInfoActivity.EXTRA_HOUSE_ID, houseOverview.getId());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return houseInfos == null ? 0 : houseInfos.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_house_photo)
        AppCompatImageView imageHousePhoto;
        @BindView(R.id.text_house_title)
        TextView textHouseTitle;
        @BindView(R.id.text_rent)
        TextView textRent;
        View root;

        public MyViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
